package com.codemart.lanze.modules.sys.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codemart.lanze.modules.sys.mapper.RolePermissionMapper;
import com.codemart.lanze.modules.sys.model.RolePermission;
import com.codemart.lanze.modules.sys.shiro.AuthenticationRealm;
import com.xiaoleilu.hutool.util.StrUtil;

/**
 * @description:
 * @author: lanze
 */
@Transactional
@Service
public class RolePermissionService extends ServiceImpl<RolePermissionMapper, RolePermission> {

	@Resource
	private AuthenticationRealm authenticationRealm;

	/**
	 * 根据角色ID获取当前角色下所有权限ID集合
	 *
	 * @param roleId
	 *            角色ID
	 * @return
	 * @throws Exception
	 */
	@Transactional(readOnly = true)
	public List<Long> findListPermissionIdsByRoleId(Long roleId) throws Exception {
		QueryWrapper<RolePermission> queryWrapper = new QueryWrapper<>();
		if (roleId != null) {
			queryWrapper.eq("role_id", roleId);
		}
		List<RolePermission> rolePermissionList = this.list(queryWrapper);

		List<Long> permissionIds = new ArrayList<>();
		for (RolePermission permission : rolePermissionList) {
			permissionIds.add(permission.getPermissionId());
		}
		return permissionIds;
	}

	/**
	 * 根据角色ID获取当前角色下所有权限ID集合，以,分割
	 *
	 * @param roleId
	 *            角色ID
	 * @return
	 * @throws Exception
	 */
	@Transactional(readOnly = true)
	public String findPermissionIdsByRoleId(Long roleId) throws Exception {
		List<Long> permissionIds = this.findListPermissionIdsByRoleId(roleId);
		return StrUtil.join(",", permissionIds);
	}

	/**
	 * 角色授权
	 *
	 * @param roleId
	 *            角色ID
	 * @param permissionIds
	 *            权限id集合
	 */
	@CacheEvict(value = "menuListCache", allEntries = true)
	public void saveOrUpdate(Long roleId, Long[] permissionIds) {
		// 先删除当前角色所拥有的权限再重现插入
		QueryWrapper<RolePermission> queryWrapper = new QueryWrapper<>();
		if (roleId != null) {
			queryWrapper.eq("role_id", roleId);
		}
		this.remove(queryWrapper);

		// 插入新的权限角色对应关系
		List<RolePermission> rolePermissionList = this.getRolePermissionList(roleId, permissionIds);
		if (rolePermissionList.size() > 0) {
			this.saveBatch(rolePermissionList);
		}

		// 清除所有用户授权缓存信息，使其重新加载
		authenticationRealm.clearCachedAuthorizationInfoAll();
	}

	/**
	 * 封装角色和权限的关系并返回
	 *
	 * @param roleId
	 * @param permissionIds
	 * @return
	 */
	private List<RolePermission> getRolePermissionList(Long roleId, Long[] permissionIds) {
		List<RolePermission> rolePermissionList = new ArrayList<>();
		RolePermission rolePermission = null;
		if (permissionIds == null) {
			return Collections.emptyList();
		}
		for (Long permissionId : permissionIds) {
			rolePermission = new RolePermission();
			rolePermission.setPermissionId(permissionId);
			rolePermission.setRoleId(roleId);
			// rolePermission.setCreateDateTime(new Date());
			// rolePermission.setModifyTime(rolePermission.getCreateTime());
			rolePermissionList.add(rolePermission);
		}
		return rolePermissionList;
	}
}
